package com.scientificInfo.server;

import com.scientificInfo.bean.*;
import com.scientificInfo.dao.*;
//import javafx.scene.shape.Circle;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class AdminServer {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    PatentMapper patentMapper;

    @Autowired
    PaperMapper paperMapper;

    @Autowired
    ProjectMapper projectMapper;

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    MessageMapper messageMapper;

    @Autowired
    WorksMapper worksMapper;

    @Autowired
    SourceMapper sourceMapper;

    @Autowired
    LoginCountMapper loginCountMapper;

    /**
     * 保存一条消息到数据库
     */
    public int insertMessage(Message message) {
        return messageMapper.insert(message);
    }

    /**
     * 根据论文id获取论文名字和教师ID
     */
    public String[] selectPaperNameById(String paperId) {
        String[] strs = new String[2];
        Paper paper = paperMapper.selectByPrimaryKey(paperId);
        strs[0] = paper.getTeacherid();
        strs[1] = paper.getPapername();
        return strs;
    }

    /**
     * 根据专利id获取论文名称和教师ID
     */
    public String[] selectPatentNameById(String patentId) {
        String[] strs = new String[2];
        Patent patent = patentMapper.selectByPrimaryKey(patentId);
        strs[0] = patent.getTeacherid();
        strs[1] = patent.getPatentname();
        return strs;
    }

    /**
     * 根据项目id获取项目名称和教师ID
     */
    public String[] selectProjectNameById(String projectId) {
        String[] strs = new String[2];
        Project project = projectMapper.selectByPrimaryKey(projectId);
        strs[0] = project.getTeacherid();
        strs[1] = project.getProjectname();
        return strs;
    }


    /**
     * 查找出所有，通过审核或者被下架的专利
     */
    public List<Patent> selectCheckAndRemovePatent() {
        PatentExample patentExample = new PatentExample();
        PatentExample.Criteria circle = patentExample.createCriteria();
        List<Integer> vals = new LinkedList<>();
        vals.add(0);
        vals.add(1);
        vals.add(3);
        circle.andCheckflagIn(vals);
        return patentMapper.selectByExample(patentExample);
    }

    /**
     * 查询所有通过审核或被下架的论文
     *
     * @return
     */
    public List<Paper> selectCheckAndRemovePaper() {
        PaperExample paperExample = new PaperExample();
        PaperExample.Criteria criteria = paperExample.createCriteria();
        List<Integer> vals = new LinkedList<>();
        vals.add(0);
        vals.add(1);
        vals.add(3);
        criteria.andCheckflagIn(vals);
        return paperMapper.selectByExample(paperExample);
    }

    /**
     * 查找所有通过审核或者被下架的项目
     */
    public List<Project> selectCheckAndRemoveProject() {
        ProjectExample projectExample = new ProjectExample();
        ProjectExample.Criteria circle = projectExample.createCriteria();
        List<Integer> vals = new LinkedList<>();
        vals.add(0);
        vals.add(1);
        vals.add(3);
        circle.andCheckflagIn(vals);
        return projectMapper.selectByExample(projectExample);
    }


    /**
     * 添加一名教师，同时开通登陆系统权限
     */
    public int insertTeacher(Teacher teacher) {
        teacherMapper.insert(teacher);
        User user = new User();
        user.setUserId(teacher.getTeacherid());
        user.setUserPaswd(teacher.getTeacherid());
        user.setUserType(3);
        return userMapper.insert(user);
    }

    public void deleteTeacher(String teacherId) {
        TeacherKey teacherKey = new TeacherKey();
        teacherKey.setTeacherid(teacherId);
        teacherMapper.deleteByPrimaryKey(teacherKey);
    }

    /**
     * 查询出所有教师
     */
    public List<Teacher> selectAllTeacher() {
        return teacherMapper.selectByExample(null);
    }

    /**
     * operaCode : 1 通过  2 不通过  3 下架
     */
    public void updatePaperStatu(String paperId, int operaCode) {
        Paper paper = paperMapper.selectByPrimaryKey(paperId);
        paper.setCheckflag(operaCode);
        paperMapper.updateByPrimaryKey(paper);
    }

    public void updatePatentStatu(String patentId, int operaCode) {
        Patent patent = patentMapper.selectByPrimaryKey(patentId);
        patent.setCheckflag(operaCode);
        patentMapper.updateByPrimaryKey(patent);
    }

    public void updateProjectStatu(String projectId, int operaCode) {
        Project project = projectMapper.selectByPrimaryKey(projectId);
        project.setCheckflag(operaCode);
        projectMapper.updateByPrimaryKey(project);
    }

    public void updateWorksStatu(String worksId, int operaCode) {
        worksMapper.updateByPrimaryKey(worksId, operaCode);
    }

    /**
     * 查询所有的
     *
     * @return
     */
    public List<Admin> getAllAdmin() {
        return adminMapper.selectByExample(null);
    }


    /**
     * 查找出所有刚上传，未经过审核的论文
     *
     * @return
     */
    public List<Paper> getAllPaperNotCheck() {
        PaperExample paperExample = new PaperExample();
        PaperExample.Criteria criteria = paperExample.createCriteria();
        criteria.andCheckflagEqualTo(0);
        List<Paper> list = paperMapper.selectByExample(paperExample);
        return list;
    }

    /**
     * 查找出所有刚上传，未经过审核的专利
     */
    public List<Patent> getAllPatentNotCheck() {
        PatentExample patentExample = new PatentExample();
        PatentExample.Criteria criteria = patentExample.createCriteria();
        criteria.andCheckflagEqualTo(0);
        List<Patent> list = patentMapper.selectByExample(patentExample);
        return list;
    }

    /**
     * 查找所有刚上传，未经过审核的项目
     */
    public List<Project> getAllProjectNotCheck() {
        ProjectExample projectExample = new ProjectExample();
        ProjectExample.Criteria criteria = projectExample.createCriteria();
        criteria.andCheckflagEqualTo(0);
        List<Project> list = projectMapper.selectByExample(projectExample);
        return list;
    }

    public int updateTeacher(String teacherId, int opeaCode) {
        int ret;
        List<Teacher> teacher = null;
        TeacherExample teacherExample = new TeacherExample();
        TeacherExample.Criteria criteria = teacherExample.createCriteria();
        List<String> idList = new LinkedList<>();
        idList.add(teacherId);
        criteria.andTeacheridIn(idList);
        teacher = teacherMapper.selectByExample(teacherExample);
        teacher.get(0).setTeacherstatu(opeaCode);
        ret = teacherMapper.updateByPrimaryKey(teacher.get(0));
        return ret;
    }


    public List<Teacher> selectTeacherNotLogOut(int status) {
        List<Teacher> teacher = null;
        teacher = teacherMapper.selectByStatus(status);
        return teacher;
    }

    public int updateAdmin(Admin admin) {
        return adminMapper.updateByPrimaryKey(admin);
    }

    public Admin selectAdminInforById(String id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    public int scorePaper(String name, int score) {
        return paperMapper.updateByScoreColumn(name, score);
    }

    public List<Works> seeAllWorks() {
        return worksMapper.queryAll();
    }

    public int scoreProject(String projectId, int score) {
        return projectMapper.scoreProject(projectId, score);
    }

    public Source isExistsPaperFile(String paperName) {
        return sourceMapper.selectPaperFile(paperName);
    }

    public int scorePatent(String patentId, int score) {
        return patentMapper.scorePatent(patentId, score);
    }

    public List<LoginCount> showLoginCountData() {

        return loginCountMapper.selectAll();
    }
}
